package com.aaa.jwt;

import com.aaa.entity.AuthUser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: JwtAuthenticationFilter
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/8/31 14:26
 * @Version:1.0
 **/
@Component
@Slf4j
public class JwtAuthenticationFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("》》》》》》》》鉴权过滤器");
        String ahthorization = httpServletRequest.getHeader("Authorization");
        System.out.println(ahthorization);
        if(null!=ahthorization){
            Claims claims = JwtTokenUtils.setToken(ahthorization);
            System.out.println(claims);
            if(claims ==null){
                filterChain.doFilter(httpServletRequest,httpServletResponse);
            }else{
                UsernamePasswordAuthenticationToken authenticationToken = getAuthUser(claims);
                System.out.println("authenticationToken----"+authenticationToken);
                boolean expiration = JwtTokenUtils.isExpiration(ahthorization);
                System.out.println(expiration);
                if(expiration){
                    filterChain.doFilter(httpServletRequest,httpServletResponse);
                }else{
                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                }
            }
        }
        filterChain.doFilter(httpServletRequest,httpServletResponse);
    }

    public static UsernamePasswordAuthenticationToken getAuthUser(Claims claims) {
        UsernamePasswordAuthenticationToken authenticationToken = null;
        try{
            // map
            Object data =claims.get("authUser");
//            System.out.println(data.);
//            实体.set（object.get）
//            new AuthUser()
            AuthUser authUser = JSON.parseObject(JSON.toJSONString(data), AuthUser.class);


            // (principal,credentials, authorities)
            // 生成认证信息对象
            authenticationToken = new UsernamePasswordAuthenticationToken(authUser,authUser.getPassword(),authUser.getAuthorities());
        }catch (Exception e){
            log.error(">>>>>>>>>>>>>解析token获取认证用户信息失败");
            e.printStackTrace();
        }
        return authenticationToken;
    }













}
